<?php

class Managepartner extends CI_Controller {

    function __construct() {
        parent::__construct();
        $id = $this->session->userdata('id');
        $adminname = $this->session->userdata('adminname');
        $hash = $this->session->userdata('hash');
        if (!empty($hash) && $hash == md5(SECRETCODE . $id . $adminname . SECRETCODE)) {
            $this->admin['name'] = $adminname;
            $this->admin['id'] = $id;
            $this->admin['hash'] = $hash;
            $this->load->model('news_m');
        } else {
            redirect(base_url() . 'admin/login');
        }
        $this->load->model('partner_m');
    }

    function index() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            $this->load->library('pagination');
            $offset = $this->uri->segment(3);
            $limit = 6;
            $config['base_url'] = site_url('admin/managepartner');
            $config['uri_segment'] = 3;
            $config['total_rows'] = $this->db->count_all('partners');
            $config['per_page'] = $limit;
            $this->pagination->initialize($config);
            $data['paginator'] = $this->pagination->create_links();

            $data['partners'] = $this->partner_m->get(0, $limit, $offset);

            $this->load->view('admin/common/header');
            $this->load->view('admin/partners', $data);
            $this->load->view('admin/common/footer');
        }
        else
            redirect(base_url() . 'admin/login');
    }

    function add() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (isset($_POST['add']) && $_POST['add'] == 'Add') {

                $data['name'] = $this->input->post('name');
                $data['description'] = $this->input->post('description');
                $data['link'] = $this->input->post('link');
                $data['thumb'] = $this->_upload_files();
                //die(print_r($data));
                if (trim($data['name']) == '')
                    redirect(base_url() . 'admin/managepartner/add');

                $id = $this->partner_m->add($data);
                if ($id > 0) {
                    redirect(base_url() . 'admin/managepartner');
                }
                else
                    die('Lỗi thêm đối tác mới!');
            }else {


                $this->load->view('admin/common/header');
                $this->load->view('admin/partner_add');
                $this->load->view('admin/common/footer');
            }
        }
        else
            redirect(base_url() . 'admin/login');
    }

    function update($id) {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (!is_numeric($id) || $id < 0)
                redirect(base_url() . 'admin/managepartner');

            if (isset($_POST['update']) && $_POST['update'] == 'Update') {
                $data['name'] = $this->input->post('name');
                $data['description'] = $this->input->post('description');
                $data['link'] = $this->input->post('link');
                $thumb = $this->_upload_files();
                if($thumb != "")
                    $data['thumb'] = $thumb;
                if (trim($data['name']) == '')
                    redirect(base_url() . 'admin/managepartner/update/' . $id);

                if ($this->partner_m->update($id, $data)) {
                    redirect(base_url() . 'admin/managepartner');
                }
                else
                    die('Lỗi cập nhật đối tác');
            }else {
                //die('xxx');
                $data['partner'] = $this->partner_m->get($id, 0, 0);
                $this->load->view('admin/common/header');
                $this->load->view('admin/partner_edit', $data);
                $this->load->view('admin/common/footer');
            }
        }
        else
            redirect(base_url() . 'admin/login');
    }

    function delete($id) {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (!is_numeric($id))
                redirect(base_url() . 'admin/managepartner');
            if ($this->partner_m->delete($id)) {
                if (file_exists('uploads/partner/' . $id))
                    if (file_exists('uploads/partner/' . $id . '/logo.jpg'))
                        unlink('uploads/partner/' . $id . '/logo.jpg');
                rmdir('uploads/partner/' . $id);
                redirect(base_url() . 'admin/managepartner');
            }
            else
                die('Lỗi xóa đối tác');
        }
        else
            redirect(base_url() . 'admin/login');
    }

    function delete_partner() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            $ids = $this->input->post('selected');
            if (is_array($ids)) {
                if ($this->partner_m->delete($ids))
                    redirect(base_url() . 'admin/managepartner');
                else
                    die('Lỗi xóa đối tác');
            }else {
                redirect(base_url() . 'admin/managepartner');
            }
        }
        else
            redirect(base_url() . 'admin/login');
    }

    public function _upload_files() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (!file_exists('uploads/partner')) {
                mkdir('uploads/partner', 0777);
                chmod('uploads/partner', 0777);
            }
            
                
                $upload_cfg['max_size'] = '2048';
                $upload_cfg['upload_path'] = 'uploads/partner';
                $upload_cfg['allowed_types'] = 'gif|jpg|png';
                $upload_cfg['encrypt_name'] = FALSE;
                $upload_cfg['overwrite'] = FALSE;               
                $this->load->library('upload', $upload_cfg);
                
               if($this->upload->do_upload('partner_logo')){ 
                        $img = $this->upload->data();  
                          return $img['file_name'];
                }
            
        } else
            redirect(base_url() . 'admin/login');
    }
    
    

}